package com.jwsoft.manager.web;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.TransportUtils;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.SSLContext;
import java.util.List;

/**
 * ES客户端配置类
 *
 * @author fangs
 */
@ConfigurationProperties(prefix = "elasticsearch")
@EnableConfigurationProperties({ElasticsearchProperties.class})
@Configuration
@Data
public class ESClientConfig {

    @Autowired
    ElasticsearchProperties elasticsearchProperties;
    @Value("${elasticsearch.fingerprint}")
    private String fingerprint;

    @Value("${elasticsearch.enabled}")
    private Boolean enabled;


    /**
     * 同步方式
     *
     * @return ElasticsearchClient ES客户端
     */
    @Bean
    public ElasticsearchClient elasticsearchClient() {
        if (!enabled) {
            return null;
        }
        SSLContext sslContext = TransportUtils
                .sslContextFromCaFingerprint(fingerprint);

        BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
        credsProv.setCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(
                        elasticsearchProperties.getUsername(), elasticsearchProperties.getPassword())
        );
        List<String> uris = elasticsearchProperties.getUris();
        HttpHost[] httpHosts = new HttpHost[uris.size()];
        for (int i = 0; i < uris.size(); i++) {
            String[] split = uris.get(i).split(":");
            String scheme = split[0];
            String host = split[1].replace("//", "");
            int port = Integer.parseInt(split[2]);
            httpHosts[i] = new HttpHost(host, port, scheme);
        }
        RestClient restClient = RestClient
                .builder(httpHosts)
                .setHttpClientConfigCallback(hc -> hc
                        .setSSLContext(sslContext)
                        .setDefaultCredentialsProvider(credsProv)
                )
                .build();

        // Create the transport and the API client
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }


}

